from django.shortcuts import render,HttpResponse
import os

def get_reportname_list():
    reportname_list = []
    reports_path = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) + "/templates/reports/peoples"
    '该文件夹下所有的文件（包括文件夹）'
    FileList = os.listdir(reports_path)
    for files in FileList:
        '原来的文件路径'
        oldDirPath = os.path.join(reports_path, files)
        '如果是文件夹则递归调用'
        if os.path.isdir(oldDirPath):
            continue
        '文件名'
        fileName = os.path.splitext(files)[0]
        reportname_list.append(fileName)
    # print(reportname_list)
    # return list(reversed(reportname_list))
    reportname_list.sort(reverse=True)
    return (reportname_list)

reportname_list = get_reportname_list()

# Create your views here.
def get_innisfree_page(request):
    # reportname_list = get_reportname_list()
    reports_name = ['newdev_1591457395','new-stg_1591681839','new-stg_1591681857']

    return render(request,'innisfree.html',{
        'reports_name':reports_name
    })

def get_detail_page(request,report_id):
    return render(request,str(report_id)+'.html')

def get_peoplesDaily_page(request):
    # return render(request,'PeoplesDaily.html')
    # reportname_list = get_reportname_list()
    return render(request, 'PeoplesDaily.html', {
            'reports_name': reportname_list
        })

def get_innisfree_report(request):
    get_value = request.GET.get('name', "")
    # reportname_list = get_reportname_list()
    reports_name = ['newdev_1591457395','new-stg_1591681839','new-stg_1591681857']
    # ll = [s for s in l if 'a' in s]
    #过滤出名字包含newdev的报告
    env_reports = list(filter(lambda x: x.find(get_value) >= 0, reports_name))
    if get_value == 'all':
        return render(request, 'innisfree.html', {
            'reports_name': reports_name
        })
    else:
        return render(request,'innisfree.html',{
            'reports_name':env_reports
        })

def get_peoples_report(request):

    get_value = request.GET.get('name', "")
    reportname_list = get_reportname_list()
    # print(reportname_list)
    # print(list(reversed(reportname_list)))
    # reportname_list_new = []
    # for i in reportname_list:
    #     reportname_list_new.append(i.replace("-", ""))
    # print(reportname_list_new)

    #过滤出名字包含选中类型的报告
    select_reports = list(filter(lambda x: x.find(get_value) >= 0, reportname_list))
    if get_value == 'all':
        return render(request, 'PeoplesDaily.html', {
            'reports_name': reportname_list
        })
    else:
        return render(request,'PeoplesDaily.html',{
            'reports_name':select_reports
        })

# def get_env_report(request):
#     # print(request.GET.get('name', ""))
#     # return HttpResponse("ok")
#
#     get_value = request.POST.get('environment', "")
#     reports_name = ['newdev_1591457395','new-stg_1591681839','new-stg_1591681857']
#     # ll = [s for s in l if 'a' in s]
#     env_reports = list(filter(lambda x: x.find(get_value) >= 0, reports_name))
#     if get_value == 'all':
#         return render(request, 'index.html', {
#             'reports_name': reports_name
#         })
#     else:
#         return render(request,'env_reports.html',{
#             'reports_name':env_reports
#         })

if __name__ == '__main__':
    pass
    # print(get_reportname_list())
    # print(os.path.abspath(os.path.dirname(os.getcwd())))



